package com.robot.orders.controller;

import com.alibaba.fastjson.JSON;
import com.robot.orders.pojo.Orders;
import com.robot.orders.service.OrdersService;
import entity.Result;
import entity.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import util.UpdateUtil;

import java.time.LocalDateTime;
import java.util.HashMap;

/**
 * orders控制器层
 * 用于支付
 *
 * @author lin
 */
@RestController
@CrossOrigin
@RequestMapping("/orders")
public class OrdersJsonController {
    @Autowired
    private OrdersService ordersService;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping(value = "third/thirdFindOrders/{page}/{size}",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    public String thirdFindOrders(@RequestBody HashMap<String,String> req, @PathVariable int page, @PathVariable int size){
        Page<Orders> ordersPage=ordersService.findSearch(req,page,size);
        logger.info("ordersPage="+ordersPage.getContent());
        return  JSON.toJSONString(ordersPage) ;
    }

    @RequestMapping(value = "third/{ordersId}/{isSoftDelete}",method = RequestMethod.DELETE,produces = "application/json; charset=utf-8")
    public void thirdDelete(@PathVariable String ordersId,@PathVariable boolean isSoftDelete){
        if(isSoftDelete){
            //软删除订单
            Orders orders=ordersService.findById(ordersId);
            if(orders!=null){
                orders.setDeletedAt(LocalDateTime.now());
                logger.info(orders.toString());
                ordersService.update(orders);
            }
        }else{
            ordersService.deleteById(ordersId);
        }
    }

    @RequestMapping(value = "third/thirdUpdateOrders",method = RequestMethod.PUT,produces = "application/json; charset=utf-8")
    public void thirdUpdateOrders(@RequestBody Orders orders){
        Orders ordersInDb=ordersService.findById(orders.getId());
        UpdateUtil.copyNonNullProperties(orders,ordersInDb);
        logger.info(ordersInDb.toString());
        ordersService.update(ordersInDb);
    }

    @RequestMapping(value = "third/countAllStatusOrders/{memberId}",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public HashMap<String,Integer> countAllStatusOrders(@PathVariable String memberId){
        return ordersService.countAllStatusOrders(memberId);
    }

}
